package com.souku.finance.application.db;

import java.sql.SQLException;
import java.util.List;
import android.content.Context;
import com.j256.ormlite.dao.Dao;

public class AvatarBeanDao
{
	private Context context;
	private Dao<AvatarBean, Integer> scheduleDaoOpe;
	private DatabaseHelper helper;

	public AvatarBeanDao(Context context)
	{
		this.context = context;
		try
		{
			helper = DatabaseHelper.getHelper(context);
			scheduleDaoOpe = helper.getDao(AvatarBean.class);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 增加一个用户
	 *
	 * @param schedule
	 * @throws SQLException
	 */
	public void add(AvatarBean schedule)
	{
		/*//事务操作
		TransactionManager.callInTransaction(helper.getConnectionSource(),
				new Callable<Void>()
				{

					@Override
					public Void call() throws Exception
					{
						return null;
					}
				});*/
		try
		{
			scheduleDaoOpe.create(schedule);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

	}

	public void update(AvatarBean schedule)
	{
		try {
			scheduleDaoOpe.update(schedule);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void delete(AvatarBean schedule)
	{
		try {
			scheduleDaoOpe.delete(schedule);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public AvatarBean get(int id)
	{
		try
		{
			return scheduleDaoOpe.queryForId(id);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public List<AvatarBean> getAll(){
		try {
			return scheduleDaoOpe.queryForAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public int isContain(String mobile){
		try {
			List<AvatarBean> beanList=scheduleDaoOpe.queryBuilder().where().eq("mobile",mobile).query();
			if(beanList.size()!=0){
				return beanList.get(0).getId();
			}else {
				return -1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	public String getUrl(String mobile){
		try {
			return scheduleDaoOpe.queryBuilder().where().eq("mobile",mobile).query().get(0).getImg_url();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
